<?php
header("Content-type: text/html; charset=utf-8");
session_start();

require_once 'common/config.php';
require_once 'common/conn.php';
require_once 'common/function.php';

require_once 'db/account.db.php';
require_once 'db/item.db.php';
require_once 'db/itemType.db.php';
require_once 'db/accountMoney.db.php';

//sprintf(param,"user_id=%s&user_pw=%s", g_BravoMain.m_cLoginID, g_BravoMain.m_cLoginPassMD5);//,pMsg->m_byErrStr, pMsg->m_byPhoneNumber);

$accountId = isset($_GET['user_id']) ? strval($_GET['user_id']) : "";
$accountPass = isset($_GET['user_pw']) ? strval($_GET['user_pw']) : "";

$dbc = new DBC();

$accountDb = new AccountDb($dbc);
$user = $accountDb->getAccount($accountId, $accountPass);
if (isArrayNotEmpty($user)) {
    $_SESSION['AccUnique'] = $user[0]['AccUnique'];
    $_SESSION['AccountID'] = $user[0]['AccountID'];
}

$itemDb = new ItemDb($dbc);

$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$adjacents = isset($_GET['adjacents']) ? intval($_GET['adjacents']) : 0;

if ($page <= 0) {
    $page = 1;
}
if ($adjacents <= 0) {
    $adjacents = 4;
}

$limit = 8;
$start = ($page - 1) * $limit;

if (isset($_GET['type'])) {
    $type = intval($_GET['type']);
} else {
    $type = "all";
}

$data = $itemDb->getItem($type, $start, $limit);

$itemTypeDb = new ItemTypeDb($dbc);
$itemTypeArray = $itemTypeDb->getItemType();

$numberOfObject = $itemDb->getItemNumber($type);
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>荣耀ol - 在线商城</title>
        <style type="text/css">
            .ttouaa{
                width:260px;
                text-align:right;
            }
            html{
                width:800px;
                height:586px;
            }
        </style>

        <meta name="description" content="Flat UI Kit Free is a Twitter Bootstrap Framework design and Theme, this responsive framework includes a PSD and HTML version."/>
        <meta name="viewport" content="width=1000, initial-scale=1.0, maximum-scale=1.0">

        <!-- Loading Bootstrap -->
        <link href="<?php echo $flatUi; ?>dist/css/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

        <!-- Loading Flat UI -->
        <link href="<?php echo $flatUi; ?>dist/css/flat-ui.css" rel="stylesheet">
        <link href="<?php echo $flatUi; ?>docs/assets/css/demo.css" rel="stylesheet">

        <link rel="shortcut icon" href="<?php echo $flatUi; ?>img/favicon.ico">

        <script src="<?php echo $flatUi; ?>js/jquery-1.11.3.js"></script>
        <script type="text/javascript">

            function addCart(itemNo) {
                $.ajax({
                    type: 'post',
                    url: 'api/cart.php',
                    data: {action: 'addCart', item_code: itemNo},
                    dataType: 'json',
                    success: function (data) {
                        console.log(data);
                        alert("已加入购物车");
                    },
                    error: function (e, f, g) {
                        console.log(e);
                    }
                });
            }

            function register() {
                location.href = "register.php";
            }
        </script>
    </head>
    <body style="width:800px;height:586px;">
        <div class="container" style="width: 800px;">
            <div class="row" style="width: 800px;">
                <div class="col-xs-3">
                    <table>
                        <tr><td>
                                <div>
                                    <?php
                                    if (isset($_SESSION['AccountID']) && $_SESSION['AccountID'] != "") {
                                        $accountMoneyDb = new AccountMoneyDb($dbc);
                                        $accountMoney = $accountMoneyDb->getAccountMoney($_SESSION['AccUnique']);
                                        $balance = 0;
                                        if ($accountMoney && isArrayNotEmpty($accountMoney)) {
                                            $balance = $accountMoney[0]['Balance'];
                                        }
                                        ?>
                                        <div class="demo-browser-side">
                                            用户：<?php echo $_SESSION['AccountID']; ?>
                                            <br>
                                            余额：<?php echo $balance; ?>
                                            <br>
                                            <a href="cart.php">查看购物车</a>
                                            <br>
                                        </div>
                                    <?php } else { ?>
                                        <form action="action/account.action.php" method="post">
                                            <div class="demo-browser-side">
                                                <div>荣耀ol - 商城登陆</div>
                                                <div class="demo-browser-action">
                                                    <input type="hidden" name="action" value="login"/>
                                                    <input type="text" class="form-control" name="username" placeholder="用户名"/>
                                                    <br>
                                                    <input type="password" class="form-control" name="password" placeholder="密码"/>
                                                    <br>
                                                    <input type="text" class="form-control" name="validatecode" value="" placeholder="验证码"/></div>
                                                <img style="width: 100px; height: 50px;"  title="点击刷新" src="api/captcha.php" align="absbottom" onclick="this.src = 'api/captcha.php?' + Math.random();"></img>
                                                <br>
                                                <br>
                                                <input type="submit" class="form-control" value="登录"/>
                                                <br>
                                                <input type="button" class="form-control" value="注册" onclick="register()"/>
                                                <br>
                                            </div>
                                    </div>
                                    </form>
                                <?php } ?>
                                </div>
                            </td></tr>
                        <tr><td> <div>
                                    <h3 class="demo-panel-title">商品分类</h3>
                                    <label class="checkbox" for="checkbox1">
                                        <a href="index.php">全部</a>
                                    </label>
                                    <?php for ($i = 0; $i < count($itemTypeArray); $i++) { ?>
                                        <label class="checkbox" for="checkbox1">
                                            <a href="index.php?type=<?php echo $itemTypeArray[$i]['id_x']; ?>"><?php echo $itemTypeArray[$i]['item_type']; ?></a>
                                        </label>
                                    <?php } ?>

                                </div></td></tr>
                    </table>


                </div>
                <div class="col-xs-9" style="margin-top: 20px;">
                    <div class="row demo-tiles">
                        <?php
                        for ($i = 0; $i < count($data); $i++) {
//                            $name = mb_convert_encoding($data[$i]['ItemName'], "UTF-8", "GBK");
                            $name = $data[$i]['ItemName'];
                            ?>
                            <div class="col-xs-3">
                                <div class="tile">
                                    <img src="<?php echo $data[$i]['ImageUrl']; ?>" alt=""
                                         style="height: 80px;
                                         margin-top: 10px;
                                         width: 80px; margin: 15px 0 12px; vertical-align: bottom;">
                                    <h3 class="tile-title-16"><?php echo $name; ?></h3>
                                    <span style="font-size: 12px;"><?php echo $data[$i]['Price']; ?>金币</span>
                                    <?php if (isset($_SESSION['AccountID']) && $_SESSION['AccountID'] != "") { ?>
                                        <a class="btn btn-primary btn-large btn-block" href="#" onclick="addCart('<?php echo $data[$i]['ItemNo'] ?>')">加入</a>
                                    <?php } else { ?>
                                        <a class="btn btn-primary btn-large btn-block" href="login.php">加入</a>
                                    <?php } ?>
                                </div>
                            </div>
                        <?php } ?>
                    </div> <!-- /tiles -->
                    <div style="width: 100%; text-align: center; padding-bottom: 20px;">
                        <?php
                        $tpages = intval($numberOfObject / $limit) + (intval($numberOfObject % $limit) == 0 ? 0 : 1);
                        $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages . "&amp;adjacents=" . $adjacents;
                        echo paginate_three($reload, $page, $tpages, $adjacents);
                        ?></div>
                </div>

            </div> <!-- /download area -->


        </div> <!-- /container -->

    </body>
</html>
